package com.platform.qujia.pay.strategy.order;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.platform.qujia.constantEnum.PayWayPayTypeEnum;
import com.platform.qujia.constantEnum.TianQuetechPayEnum;
import com.platform.qujia.dao.PayWayMapper;
import com.platform.qujia.param.request.TradeReverseScanPayParameter;
import com.platform.qujia.param.response.YunhuaPayRes;
import com.platform.qujia.param.response.YunhuaResponse;
import com.platform.qujia.pay.QueryOrderStrategy;
import com.platform.qujia.pay.strategy.YunhuaChannel;
import com.platform.qujia.pojo.PayWay;
import com.platform.qujia.pojo.TradePaymentRecordsDO;
import com.platform.qujia.service.PayOrderService;
import com.platform.qujia.utils.exception.CustomException;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @date 2023/8/11
 */
@Component
public class YunhuaQueryOrder implements QueryOrderStrategy {

    private static final Logger log = LoggerFactory.getLogger(YunhuaQueryOrder.class);

    private final TypeReference<YunhuaResponse<YunhuaPayRes>> clazz = new TypeReference<YunhuaResponse<YunhuaPayRes>>() {};

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private PayWayMapper payWayMapper;
    private String queryOrderUrl;

    private YunhuaChannel yunhuaChannel;

    public void setQueryOrderUrl(String queryOrderUrl) {
        this.queryOrderUrl = queryOrderUrl;
    }

    public void setYunhuaChannel(YunhuaChannel yunhuaChannel) {
        this.yunhuaChannel = yunhuaChannel;
    }

    @Override
    public TianQuetechPayEnum getOrderStatus(TradeReverseScanPayParameter parameter, TradePaymentRecordsDO record, PayWay payWay) {
        QueryOrderForm form = new QueryOrderForm();
        form.setFmno(payWay.getMno());
        form.setClientOrderNo(record.getOrderSn());
        form.setClientSerialNo(record.getOrderSn());

        JSONObject jsonParam = yunhuaChannel.signParam((JSONObject) JSONObject.toJSON(form),payWay.getSecretKey());
        log.info("云化支付订单查询参数:{}",jsonParam.toJSONString());
        ResponseEntity<String> response = restTemplate.postForEntity(queryOrderUrl, jsonParam, String.class);
        String body = response.getBody();
        log.info("云化支付订单查询结果:{}",body);
        YunhuaResponse<YunhuaPayRes> res = JSONObject.parseObject(body, this.clazz);
        if (res == null ||  !StringUtils.equals(YunhuaChannel.RES_CODE_SUCCESS,res.getCode())) {
            return null;
        }
        TianQuetechPayEnum payEnum;
        Integer status = res.getData().getStatus();
        switch (status){
            case 1:
            case 2:
                payEnum = TianQuetechPayEnum.PAY_PAYING;
                break;
            case 3:
                payEnum = TianQuetechPayEnum.PAY_SUCCESS;
                break;
            case 4:
                payEnum = TianQuetechPayEnum.PAY_FAIL;
                break;
            case 5:
                payEnum = TianQuetechPayEnum.PAY_REFUNDED;
                break;
            default:
                payEnum = null;

        }
        return payEnum;
    }

    @Getter
    @Setter
    static class QueryOrderForm{
        private String fmno;
        private String clientOrderNo;
        private String clientSerialNo;
    }
}
